<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CarPackageOrderAttr;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CarPackageOrderAttrController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $request = request();

        return Grid::make(CarPackageOrderAttr::with(['order']), function (Grid $grid) use($request) {
            if ($request->has('order_id')) {
                $order_id = $request->get('order_id');
                $grid->tools('<a class="btn btn-primary" href="/admin/car-package-used-log?order_id='.$order_id.'">全套餐使用记录</a>');

                $grid->model()->where('order_id', $order_id);
            } else {
                $grid->tools('<a class="btn btn-primary" href="/admin/car-package-used-log">全套餐使用记录</a>');
            }

            if ($request->has('user_id')) {
                $grid->model()->where('user_id', $request->get('user_id'));
            }

//            $grid->column('id')->sortable();
            $grid->column('order.id', admin_trans('car-package-order-attr.fields.order_id'));
            $grid->column('order.user_name', admin_trans('car-package-order-attr.fields.user_id'));
//            $grid->column('package_title', admin_trans('car-package-order-attr.fields.package_title'));
//            $grid->column('package_pic')->gallery();
            $grid->column('attr_title');
            $grid->column('total_number')->sortable();
            $grid->column('usable_number')->sortable();
            $grid->column('used_number')->sortable();
            $grid->column('updated_at')->sortable();
            $grid->export();

            $grid->model()->orderBy('id', 'desc');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('attr_title');
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append("<a href='/admin/car-package-used-log?attr_id={$actions->getKey()}'>使用记录</a>");
            });

            $grid->disableEditButton();
            $grid->disableViewButton();
            $grid->disableDeleteButton();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, CarPackageOrderAttr::with(['order']), function (Show $show) {
            $show->field('id');
            $show->field('order.user_name', admin_trans('car-package-order-attr.fields.user_id'));
            $show->field('order.order_sn', admin_trans('car-package-order-attr.fields.order_sn'));
            $show->field('package_title');
//            $show->field('package_pic')->image();
            $show->field('attr_title');
            $show->field('total_number');
            $show->field('usable_number');
            $show->field('used_number');
            $show->field('created_at');
            $show->field('updated_at');

            $show->divider();

            $show->panel()->tools(function ($tools) {
                $tools->disableDelete();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CarPackageOrderAttr(), function (Form $form) {
            $form->display('id');
            $form->text('attr_title')->disable();
            $form->text('total_number')->disable();
            $form->text('usable_number');
            $form->text('used_number');
        
            $form->display('created_at');
            $form->display('updated_at');

            $form->tools(function (Form\Tools $tools) {
                $tools->disableDelete();
            });
        });
    }
}
